package com.ph.irp.base.assistantmaterial.action;

import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.assistantmaterial.service.PBaseAssistantmaterialDeliveryaddressService;
import com.ph.irp.model.PBaseAssistantmaterialDeliveryaddress;

public class AssistantmaterialDeliveryaddressAction extends ActionSupport {
	private PBaseAssistantmaterialDeliveryaddressService deliveryAddressService;

	public void setDeliveryAddressService(
			PBaseAssistantmaterialDeliveryaddressService deliveryAddressService) {
		this.deliveryAddressService = deliveryAddressService;
	}

	private List<PBaseAssistantmaterialDeliveryaddress> deliveryAddresses;
	private PBaseAssistantmaterialDeliveryaddress deliveryAddress;
	private Object[] rowlist = null;

	public List<PBaseAssistantmaterialDeliveryaddress> getDeliveryAddresses() {
		return deliveryAddresses;
	}

	public void setDeliveryAddresses(
			List<PBaseAssistantmaterialDeliveryaddress> deliveryAddresses) {
		this.deliveryAddresses = deliveryAddresses;
	}

	public PBaseAssistantmaterialDeliveryaddress getDeliveryAddress() {
		return deliveryAddress;
	}

	public void setDeliveryAddress(
			PBaseAssistantmaterialDeliveryaddress deliveryAddress) {
		this.deliveryAddress = deliveryAddress;
	}

	public Object[] getRowlist() {
		return rowlist;
	}

	public void setRowlist(Object[] rowlist) {
		this.rowlist = rowlist;
	}

	public String showDeliveryAddressesList() throws Throwable {
		deliveryAddress = null;
		deliveryAddresses = deliveryAddressService.getDeliveryaddressList();
		return "success";
	}

	@SuppressWarnings("unchecked")
	public String searchDeliveryAddress() throws Throwable {
		if (deliveryAddress == null) {
			deliveryAddresses = deliveryAddressService.getDeliveryaddressList();
		} else {
			if (!deliveryAddress.getDeliveryAddressName().equals("")
					&& !deliveryAddress.getDeliveryAddressCode().equals("")) {
				deliveryAddresses = deliveryAddressService
						.getDeliveryaddressByNameAndCode(deliveryAddress
								.getDeliveryAddressName(), deliveryAddress
								.getDeliveryAddressCode());
			} else if (!deliveryAddress.getDeliveryAddressName().equals("")) {
				deliveryAddresses = deliveryAddressService
						.getDeliveryaddressByName(deliveryAddress
								.getDeliveryAddressName());
			} else if (!deliveryAddress.getDeliveryAddressCode().equals("")) {
				deliveryAddresses = deliveryAddressService
						.getDeliveryaddressByCode(deliveryAddress
								.getDeliveryAddressCode());
			} else {
				deliveryAddresses = deliveryAddressService
						.getDeliveryaddressList();
			}
		}
		deliveryAddress = null;
		return "success";
	}

	// List页面跳转到add新增页面
	public String addInputDeliveryAddress() {
		deliveryAddress = null;
		return "success";
	}

	// List页面跳转到update修改页面
	public String updateInputDeliveryAddress() throws Throwable {
		Integer deliveryAddressId = Integer.valueOf((String) rowlist[0]);
		deliveryAddress = deliveryAddressService
				.getDeliveryaddressById(deliveryAddressId);
		return "success";
	}

	public String addDeliveryAddress() throws Throwable {
		deliveryAddressService.addDeliveryaddress(deliveryAddress);
		return "success";
	}

	public String updateDeliveryAddress() throws Throwable {
		deliveryAddressService.updateDeliveryaddress(deliveryAddress);
		deliveryAddress = null;
		return "success";
	}

	public String deleteDeliveryAddress() throws Throwable {
		for (int i = 0; i < rowlist.length; i++) {
			deliveryAddressService.deleteDeliveryaddress(Integer
					.valueOf((String) rowlist[i]));
		}
		return "success";
	}

	@SuppressWarnings("unchecked")
	public String selectDeliveryAddress() throws Throwable {
		if (deliveryAddress == null) {
			deliveryAddresses = deliveryAddressService.getDeliveryaddressList();
		} else {
			if (!deliveryAddress.getDeliveryAddressName().equals("")
					&& !deliveryAddress.getDeliveryAddressCode().equals("")) {
				deliveryAddresses = deliveryAddressService
						.getDeliveryaddressByNameAndCode(deliveryAddress
								.getDeliveryAddressName(), deliveryAddress
								.getDeliveryAddressCode());
			} else if (!deliveryAddress.getDeliveryAddressName().equals("")) {
				deliveryAddresses = deliveryAddressService
						.getDeliveryaddressByName(deliveryAddress
								.getDeliveryAddressName());
			} else if (!deliveryAddress.getDeliveryAddressCode().equals("")) {
				deliveryAddresses = deliveryAddressService
						.getDeliveryaddressByCode(deliveryAddress
								.getDeliveryAddressCode());
			} else {
				deliveryAddresses = deliveryAddressService
						.getDeliveryaddressList();
			}
		}
		deliveryAddress = null;
		return "success";
	}
}
